package cn.edu.hgu.dao.impl;

import cn.edu.hgu.dao.NewsDao;
import cn.edu.hgu.entity.News;
import cn.edu.hgu.util.JdbcUtil;

import java.util.List;

/**
 * 新闻接口数据层
 */
public class NewsDaoImpl implements NewsDao {

    /**
     * 获取所有的用户信息
     * @param news
     * @return
     */
    @Override
    public List<News> getListAll(News news) {
        String sql="select * from news where 1=1 ";
        if(news.getTitle()!=null&&!news.getTitle().equals("")){
            sql+=" and title like '%"+news.getTitle()+"%' ";
        }
        if(news.getContent()!=null&&!news.getContent().equals("")){
            sql+=" and content like '%"+news.getContent()+"%' ";
        }
        if(news.getType()!=null&&!news.getType().equals("")){
            sql+=" and type like '%"+news.getType()+"%' ";
        }
        if(news.getPublisher()!=null&&!news.getPublisher().equals("")){
            sql+=" and publisher like '%"+news.getPublisher()+"%' ";
        }
        if(news.getDepartment()!=null&&!news.getDepartment().equals("")){
            sql+=" and department like '%"+news.getDepartment()+"%' ";
        }
        List<News> newsList= JdbcUtil.executeQuery(sql,null,News.class);
        return newsList;
    }

    @Override
    public int add(List<Object> news) {
        String sql="insert into news(title,content,type,publisher,department,count) values(?,?,?,?,?,?) ";
        int count=JdbcUtil.executeUpdate(sql, news);
        return count;
    }

    @Override
    public int del(List<Object> news) {
        String sql="delete from news  where id=? ";
        int count=JdbcUtil.executeUpdate(sql, news);
        return count;
    }

    @Override
    public News getId(String id) {
        String sql="select * from news where id="+id;
        List<News> users= JdbcUtil.executeQuery(sql,null,News.class);
        return users.get(0);
    }

    @Override
    public int update(List<Object> news) {
        String sql="update news set title=?,content=?,type=?,publisher=?,department=?,count=? where id=? ";
        int count=JdbcUtil.executeUpdate(sql, news);
        return count;
    }


}
